﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SenerPro;

namespace MensUnoShop.Models.DBA
{
    public class DBNews
    {
        private DataClassesMenDataContext _dc;
        private Logger _log = new Logger(Config.logpath);

        public List<UNews> GetNewsList(bool hide)
        {
            List<UNews> nlist = new List<UNews>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var nl = _dc.UNews.OrderByDescending(n=>n.Nid);

                    if (nl != null)
                    {
                        if (hide)
                            nlist = nl.Where(n => n.Nstatus).ToList();
                        else
                            nlist = nl.ToList();
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetNewsList", ex.ToString());
            }
            return nlist;
        }

        public void ChangeNewsStatus(int nid)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var nl = _dc.UNews.SingleOrDefault(n => n.Nid==nid);
                    if (nl != null)
                    {
                        nl.Nstatus = !nl.Nstatus;
                        _dc.SubmitChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "ChangeNewsStatus", ex.ToString());
            }
        }

        public UNews GetNews(int nid)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var nl = _dc.UNews.SingleOrDefault(n => n.Nid == nid);
                    if (nl != null)
                    {
                        return nl;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetNews", ex.ToString());
            }
            return null;
        }

        public void UptNews(int id, string Ntitle, string Ncontent)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var nl = _dc.UNews.SingleOrDefault(n => n.Nid == id);
                    if (nl != null)
                    {
                        nl.Ntitle = Ntitle;
                        nl.Ncontent = Ncontent;
                        _dc.SubmitChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "UptNews", ex.ToString());
            }
        }

        public bool AddNews(string Ntitle, string Ncontent)
        {
            bool ischeck = false;
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    UNews un = new UNews();
                    un.Ndatein = DateTime.Today;
                    un.Nstatus = true;
                    un.Ntitle = Ntitle;
                    un.Ncontent = Ncontent;
                    _dc.UNews.InsertOnSubmit(un);
                    _dc.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "UptNews", ex.ToString());
            }
            return ischeck;
        }

        public void DelNews(int nid)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    UNews un = _dc.UNews.SingleOrDefault(n => n.Nid == nid);
                    if (un != null)
                    {
                        _dc.UNews.DeleteOnSubmit(un);
                        _dc.SubmitChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "DelNews", ex.ToString());
            }
        }
    }
}